﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace syc.Jidong
{
    public partial class 设备编码表管理 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {

            }
            else
            {

            }
            this.Panel_Msg.Visible = false;
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //this.Label_ID.Text = this.GridView1.SelectedDataKey[0].ToString();
        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = false;
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            this.Panel_Msg.Visible = true;
            int bm=0;

            int.TryParse(this.BianMa.Text,out bm);
            if (bm > 99 || bm < 1)
            {
                this.Message.Text = "·编码录入有误";
                return;
            }
            string bms = bm.ToString();
            if (bm < 10) bms = "0" + bms;
            bms = this.Label_ShangJi.Text + bms;

            string mc = this.MingCheng.Text.Trim();
            mc = mc.Replace("'", "");
            if (mc=="")
            {
                this.Message.Text = "·名称录入有误";
                return;
            }
            int zc = 0;
            if (this.ZhiChan.Checked) zc = 1;
            this.SqlDataSource1.InsertCommand = "if not exists(select 编码 from 设备编码 where 编码='"
                + bms + "' and 资产=" + zc
                + ") \n begin\n insert into 设备编码(编码,名称,资产)values('"
                + bms + "','" + mc + "'," + zc + ")\n end";
            if (this.SqlDataSource1.Insert() > 0)
            {
                this.Panel2.Visible = false;
                this.Message.Text = "·保存成功";
            }
            else
            {
                this.Message.Text = "·保存失败，请检查编码是否重复。";
            }
            
        }

        protected void Button_New_Click(object sender, EventArgs e)
        {
            this.Label_ShangJi.Text = "";
            this.BianMa.Text = "";
            this.MingCheng.Text = "";
            this.Panel2.Visible = true;
            this.ZhiChan.Checked = false;
        }

        protected void Button_New2_Click(object sender, EventArgs e)
        {
            this.Label_ShangJi.Text = "";
            this.BianMa.Text = "";
            this.MingCheng.Text = "";
            this.Panel2.Visible = true;
            this.ZhiChan.Checked = true;
        }

        protected void Button_KZ_Click(object sender, EventArgs e)
        {
            string[] t=((Button)sender).CommandName.Split(new char[]{'|'});
            this.Label_ShangJi.Text = t[0];
            if (t[1] == "0") this.ZhiChan.Checked = false;
            else this.ZhiChan.Checked = true;
            this.BianMa.Text = "";
            this.MingCheng.Text = "";
            this.Panel2.Visible = true;
            
        }
    }
}
